﻿using KingsSharp.Common;
using KingsSharp.Common.Data;
using KingsSharp.Common.Extensions;
using CRM.Admin.Contracts.Dto.Sys;
using CRM.Admin.Contracts.Model.Sys;
using System;
using System.Linq;


namespace CRM.Admin.Services.Service.Sys
{
    public partial class MenuService
    {
        #region 菜单目录
        /// <summary>
        /// 获取 菜单目录信息
        /// </summary>
        public IQueryable<MenuCatalog> MenuCatalogs
        {
            get { return _menucatologRepository.Entities; }
        }

        /// <summary>
        /// 添加菜单目录
        /// </summary>
        /// <param name="dtos">包含添加信息的菜单目录信息</param>
        /// <returns></returns>
        public OperationResult AddMenuCatalog(params MenuCatalogDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            return _menucatologRepository.Insert(dtos, dto =>
            {
                if (dto.Pid.HasValue)
                {
                    if (_menucatologRepository.ExistsCheck(m => m.Name == dto.Name && m.ParentMenuCatalog != null && m.ParentMenuCatalog.Id == dto.Pid.Value))
                    {
                        throw new Exception("同目录下“{0}”的目录已存在，不能重复添加。".FormatWith(dto.Name));
                    }
                }
                else
                {
                    if (_menucatologRepository.ExistsCheck(m => m.Name == dto.Name && m.ParentMenuCatalog != null))
                    {
                        throw new Exception("无上级目录“{0}”的目录已存在，不能重复添加。".FormatWith(dto.Name));
                    }
                }
            },(dto, entity) => {
                if (dto.Pid.HasValue)
                {
                    MenuCatalog catalog = _menucatologRepository.GetByKey(dto.Pid.Value);
                    if (catalog == null)
                    {
                        throw new Exception("要加入的上级目录不存在");
                    }
                    entity.ParentMenuCatalog = catalog;
                }
                return entity;
            });
        }

        /// <summary>
        /// 更新菜单目录信息
        /// </summary>
        /// <param name="dtos">包含添加信息的菜单目录信息</param>
        /// <returns></returns>
        public OperationResult EditMenuCatalog(params MenuCatalogDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            return _menucatologRepository.Update(dtos, dto =>
            {
                if (dto.Pid.HasValue)
                {
                    if (_menucatologRepository.ExistsCheck(m => m.Id != dto.Id && m.Name == dto.Name && m.Type == dto.Type && m.ParentMenuCatalog != null && m.ParentMenuCatalog.Id == dto.Pid.Value))
                    {
                        throw new Exception("同目录下“{0}”的目录已存在，无法更新。".FormatWith(dto.Name));
                    }
                }
                else
                {
                    if (_menucatologRepository.ExistsCheck(m => m.Id != dto.Id && m.Name == dto.Name && m.Type==dto.Type && m.ParentMenuCatalog == null))
                    {
                        throw new Exception("无上级目录“{0}”的目录已存在，无法更新。".FormatWith(dto.Name));
                    }
                }
            });
        }

        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="ids">要删除的目录编号</param>
        /// <returns>业务操作结果</returns>
        public OperationResult DeleteMenuCatalogs(params Guid[] ids)
        {
            ids.CheckNotNull("ids");
            return _menucatologRepository.Delete(ids);
        }
        #endregion
    }
}
